import tool from '../tool'
export default{
    getter(_this, key, module = 'global') {

        if (key.indexOf('.') !== -1) {
            let [k1, k2] = key.split('.');
            key = `${module}/${k1}`;
            let rs = _this.$store.getters[key];
            return  rs[k2] ;
        } else {
            key = `${module}/${key}`;
            return  _this.$store.getters[key];
        }
    },
    get(_this, key, module = 'global') {
        if (key.indexOf('.') !== -1) {
            let [k1, k2] = key.split('.');
            return module ? _this.$store.state[module][k1][k2] : _this.$store.state[k1][k2];
        } else {
            return module ? _this.$store.state[module][key] : _this.$store.state[key];
        }
    },
    commit(_this, mutation, param, module = 'global'){
        mutation = `${module}/${mutation}`;
        _this.$store.commit(mutation, param);
    },
    push(_this, mutation, state, key, val, module = 'edit'){
        mutation = `${module}/${mutation}`;
        if (key === false) {
            _this.$store.commit(mutation, val);
        } else {
            let arr = this.get(_this, state, module)
            arr[key] = val
            _this.$store.commit(mutation, arr);
        }

    },
    dispatch(_this, action, val, module = 'global'){
        action = `${module}/${action}`;
        _this.$store.dispatch(action, val);
    },
    saveResult(_this, key, val){
        this.push(_this, 'setRs', 'rs', key, val, 'com');
    },
    getResult(_this, key){
        key = `rs.${key}`;
        return this.get(_this, key, 'com');
    },
    getEditType(_this){
        let isinEditModal = this.get(_this, 'isinEditModal', 'edit');
        let isinOtherEdit = this.get(_this, 'isinOtherEdit', 'edit');
        let type = isinEditModal ? 1 : (isinOtherEdit ? 2 : 3);
        return `EditType_${type}`;
    },
    getModelByMid(_this, mid){
        let models = this.get(_this, 'model_all')||[];
        let model = models.find(model => {
            return model.id == mid
        });
        return model;
    },
    getModelByCtl(_this, ctl){
        let models = this.get(_this, 'model_all')||[];
        let model = models.find(model => {
            return model.name == ctl
        });
        return model;
    }
}
